(function () {

    'use strict';

    angular
    .module('app')
    .factory('APICartService', APICartService);

    APICartService.$inject =  ['$http', '$q', '$timeout', 'CacheFactory', 'ENUM'];

    function APICartService($http, $q, $timeout, CacheFactory, ENUM) {

        var service = new APIEndpoint( $http, $q, $timeout, CacheFactory, 'APICartService' );

        service.getCartNum = function(params) {
            return this.fetch( '/weixin/goods/getCartNum', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.cartList = function(params) {
            return this.fetch( '/weixin/goods/cartList', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.addCart = function(params) {
            return this.fetch( '/weixin/goods/addCart', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.deleteCart = function(params) {
            return this.fetch( '/weixin/goods/deleteCart', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? true : false;
            });
        };

        service.updateNum = function(params) {
            return this.fetch( '/weixin/goods/updateNum', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.clear = function(params) {
            return this.fetch( 'ecapi.order.goods.card', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.checkoutCart = function(params) {
            return this.fetch( '/weixin/order/checkoutCart', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.purchase = function( params ) {
            return this.fetch( '/weixin/order/purchase', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        service.prePurchase = function( params ) {
            return this.fetch( '/weixin/order/prePurchase', params, false, function(res){
                return ENUM.ERROR_CODE.OK == res.data.code ? res.data.data : null;
            });
        };

        return service;

    }

})();
